import Vue from 'vue'
var methods = {
    unitAjax: function(type, url, data, success, failed, async = 'true') {
        var sign = '';
        // 创建ajax对象
        var xhr = null;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            xhr = new ActiveXObject('Microsoft.XMLHTTP')
        }
        var type = type.toUpperCase();

        // 用于清除缓存
        var random = Math.random();

        //获取用户id
        // data.userId = methods.getValue("userId") == null ? "" : methods.getValue("userId");
        //获取token
        // data.token = methods.getValue("token") == null ? "" : methods.getValue("token");


        if (typeof data == 'object') {

            if (type != 'POST') {

                if (data) {
                    let geturl = ''
                    for (let key in data) {
                        geturl = geturl + key + '=' + data[key] + '&';
                    }
                    geturl = geturl.substr(0, geturl.length - 1);
                    url = url + '?' + geturl
                    xhr.open(type, url, true);
                }
                xhr.send();

            } else if (type == 'POST') {
                xhr.open('POST', url, async);
                // 如果需要像 html 表单那样 POST 数据，请使用 setRequestHeader() 来添加 http 头。
                xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
                xhr.send(JSON.stringify(data));
            }

            // 处理返回数据
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        success(JSON.parse(xhr.responseText));
                        let code = JSON.parse(xhr.responseText).code;
                        // if (code == 403) {
                        //     this.$message('请先设置笔名信息')
                        //     setTimeout(() => {
                        //         this.$router.push({ path: '/login', query: { penName: false } })
                        //     }, 3000);

                        // } else if (code == 300) {
                        //     this.$message('登录过期')
                        //     setTimeout(() => {
                        //         this.$router.push({ path: '/login', query: { penName: true } })
                        //     }, 3000);
                        // } else if (code == 304) {
                        //     this.$message('登录超时')
                        //     setTimeout(() => {
                        //         this.$router.push({ path: '/login', query: { penName: true } })
                        //     }, 3000);
                        // } else if (code == 203) {
                        //     this.$router.push({ path: '/forbid', query: { userId: this.$route.query.userId } })
                        // }

                    } else {
                        if (failed) {
                            failed(xhr.status);
                        }
                    }
                }
            }
        }
    },
    // // touch
    // touch(event) {
    //     var event = event || window.event;
    //     var oInp = document.getElementById("inp");
    //     switch (event.type) {
    //         case "touchstart":
    //             oInp.innerHTML = "Touch started (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")";
    //             break;
    //         case "touchend":
    //             oInp.innerHTML = "<br>Touch end (" + event.changedTouches[0].clientX + "," + event.changedTouches[0].clientY + ")";
    //             break;
    //         case "touchmove":
    //             event.preventDefault();
    //             oInp.innerHTML = "<br>Touch moved (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")";
    //             break;
    //     }

    // },
    //->设置
    setValue: function(options) {
        var _default = {
            name: null,
            value: null,
            expires: new Date(new Date().getTime() + (30 * 1000 * 60 * 60 * 24)),
            path: '/'
        };
        for (var key in options) {
            if (options.hasOwnProperty(key)) {
                _default[key] = options[key];
            }
        }
        // document.cookie = _default.name + "=" + escape(_default.value) + ";expires=" + _default.expires.toGMTString() + ";path=" + _default.path + ";domain=" + _default.domain;
        document.cookie = _default.name + "=" + escape(_default.value) + ";expires=" + _default.expires.toGMTString() + ";path=" + _default.path;

    },

    //->获取
    getValue: function(name) {
        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
        if (arr != null) {
            return unescape(arr[2]);
        }
        return null;

    },

    //->删除
    removeValue: function(options) {
        var _default = {
            name: null,
            path: '/'
        };
        for (var key in options) {
            if (options.hasOwnProperty(key)) {
                _default[key] = options[key];
            }

        }
        if (methods.getValue(_default.name)) {
            // document.cookie = _default.name + "= ;path=" + _default.path + ";domain=" + _default.domain + ";expires=Fri,02-Jan-1970 00:00:00 GMT";
            document.cookie = _default.name + "= ;path=" + _default.path + ";expires=Fri,02-Jan-1970 00:00:00 GMT";

        }

    },

}
export default methods;